const respond = require('../utils/respond')
const writeErrLog = require('../utils/writeErrLog')
const jwt = require('jsonwebtoken')
module.exports = (req, res, next) => {

    if (!req.headers.authorization) {
        respond(res, 3, '缺少令牌！')
        return
    }
    let tokenData = jwt.verify(req.headers.authorization, 'abcdefg')

    try {
        tokenData
    } catch (err) {
        writeErrLog(err)
        respond(res, 4, '令牌是错误的')
        return
    }
    if (tokenData.startTime + tokenData.expires < +new Date()) {
        respond(res, 5, '令牌过期了')
        return
    }
    req.tokenData = tokenData
    next()
}